home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Gamer Resource Kit / Hardcore Gamer Resource Kit - Disc 2.iso / Utils / UNIX / UNZIP520 / WINGUI / COPYFILE.C < prev    next >
C/C++ Source or Header  |  1996-01-23  |  10KB  |  311 lines

  1. #include <string.h>
  2. #include <io.h>
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #define UNZIP_INTERNAL
  6. #include "unzip.h"
  7. #include "wingui\wizunzip.h"
  8. #include "wingui\helpids.h"
  9. #ifndef WIN32
  10. #include <lzexpand.h>
  11. #endif                           
  12.  
  13. void StripDirectory(LPSTR lpDir);
  14.  
  15. static char szTargetDirName[128] = "";
  16. static char __based(__segname("STRINGS_TEXT")) szCantCopyFile[] =
  17.    "Internal error: Cannot copy file. Common dialog error code is 0x%lX.";
  18.  
  19. #ifdef __BORLANDC__
  20. #pragma warn -par
  21. #endif
  22. BOOL WINAPI CopyFileProc(HWND hDlg, WORD wMessage, WPARAM wParam, LPARAM lParam)
  23. {
  24.     static OPENFILENAME __far *lpofn = 0;
  25.     static WORD wClose;
  26.  
  27.     switch (wMessage)
  28.     {
  29.     case WM_DESTROY:
  30.         if (lpofn && lpofn->lpstrFile && (wClose == IDOK))
  31.         {
  32.          LPSTR lpszSeparator;
  33.  
  34.         /* If we have a trailing '\\' we need to get it */
  35.         GetDlgItemText(hDlg, edt1, lpofn->lpstrFile, WIZUNZIP_MAX_PATH);
  36.  
  37.         /* Flag whether there is a trailing backslash */
  38.         lpszSeparator = lpofn->lpstrFile+lstrlen(lpofn->lpstrFile)-1;
  39.         if (*lpszSeparator == '\\')
  40.             uf.fTrailingSlash = TRUE;
  41.         }
  42.         break;
  43.     case WM_INITDIALOG:
  44.         {
  45.             RECT    rT1, rT2;
  46.             short   nWidth, nHeight;
  47.             HWND hTemp;
  48.  
  49.             lpofn = (OPENFILENAME __far *)lParam;
  50.             CenterDialog(GetParent(hDlg), hDlg); /* center on parent */
  51.  
  52.             /* Disable the file type label
  53.                and the file type combo box
  54.  
  55.                Note: stc2, cmb1 etc are defined in DLGS.H in the standard
  56.                windows include files.
  57.             */
  58.             hTemp = GetDlgItem(hDlg, stc2);
  59.             WinAssert(hTemp);
  60.             EnableWindow(hTemp, FALSE);
  61.             ShowWindow(hTemp, SW_HIDE);
  62.  
  63.             hTemp = GetDlgItem(hDlg, cmb1);
  64.             WinAssert(hTemp);
  65.             EnableWindow(hTemp, FALSE);
  66.             ShowWindow(hTemp, SW_HIDE);
  67.  
  68.             hTemp = GetDlgItem(hDlg, cmb2);
  69.             WinAssert(hTemp);
  70.             GetWindowRect(hTemp, &rT1);
  71.  
  72.             /* Extend the rectangle of the list of files
  73.                in the current directory so that it's flush
  74.                with the bottom of the Drives combo box
  75.             */
  76.             hTemp = GetDlgItem(hDlg, lst1);
  77.             WinAssert(hTemp);
  78.             GetWindowRect(hTemp, &rT2);
  79.             nWidth = (short)(rT2.right - rT2.left);
  80.             nHeight = (short)(rT1.bottom - rT2.top);
  81.             ScreenToClient(hDlg, (LPPOINT)&rT2);
  82.             MoveWindow(hTemp,
  83.                         rT2.left, rT2.top,
  84.                         nWidth,
  85.                         nHeight,
  86.                         TRUE);
  87.         }
  88.     default:
  89.         break;
  90.     }
  91.  
  92.     /* message not handled */
  93.     return 0;
  94. }
  95. #ifdef __BORLANDC__
  96. #pragma warn .par
  97. #endif
  98.  
  99. /* Strip off the directory leaving only the file name */
  100. void StripDirectory(LPSTR lpDir)
  101. {
  102. LPSTR lpchLast, i, j;
  103. /* If no '\\' then simply return */
  104. if ((lpchLast = lstrrchr(lpDir, '\\')) == 0)
  105.    {
  106.    return;
  107.    }
  108. if ((lpchLast = lstrrchr(lpDir, '\\'))!=0)
  109.    {
  110.    for (i = lpchLast+1, j = lpDir; *i; i++, j++)
  111.       {
  112.       *j = *i;
  113.       }
  114.    *j = '\0';
  115.    }
  116.  
  117. else if ((lpchLast = lstrrchr(lpDir, ':'))!=0)
  118.    {
  119.    for (i = lpchLast+1, j = lpDir; *i; i++, j++)
  120.       {
  121.       *j = *i;
  122.       }
  123.    *j = '\0';
  124.    }
  125. }
  126.  
  127. void CopyArchive(HWND hWnd, BOOL move_flag, BOOL rename_flag)
  128. {
  129. char tempFileName[WIZUNZIP_MAX_PATH], tempPath[WIZUNZIP_MAX_PATH];
  130. #ifndef WIN32
  131.    FARPROC lpfnSelectDir;
  132. #endif
  133.  
  134. #ifndef WIN32
  135. _fmemset(&lpumb->ofn, '\0', sizeof(OPENFILENAME)); /* initialize struct */
  136. #else
  137. memset(&lpumb->ofn, '\0', sizeof(OPENFILENAME)); /* initialize struct */
  138. #endif
  139. WinAssert(hWnd);
  140. lpumb->ofn.lStructSize = sizeof(OPENFILENAME);
  141. lpumb->ofn.hwndOwner = hWnd;
  142. lpumb->ofn.hInstance = hInst;
  143. lpumb->ofn.lpstrFilter = "All Files (*.*)\0*.*\0\0";
  144. lpumb->ofn.nFilterIndex = 1;
  145.  
  146.                /* strip off filename */
  147. #ifdef __BORLANDC__
  148. #pragma warn -def
  149. #endif
  150. OemToAnsi(lpumb->szFileName, lpumb->szUnzipToDirNameTmp);
  151. #ifdef __BORLANDC__
  152. #pragma warn .def
  153. #endif
  154.                 /* Strip off path */
  155. StripDirectory(lpumb->szUnzipToDirNameTmp);
  156.                 /* Now we've got just the file name */
  157. lpumb->ofn.lpstrFile = lpumb->szUnzipToDirNameTmp; /* result goes here! */
  158.                 /* Save file name only */
  159. lstrcpy(tempFileName, lpumb->szUnzipToDirNameTmp);
  160. lpumb->ofn.nMaxFile = WIZUNZIP_MAX_PATH;
  161. lpumb->ofn.lpstrFileTitle = NULL;
  162. lpumb->ofn.nMaxFileTitle = OPTIONS_BUFFER_LEN; /* ignored ! */
  163. if (move_flag)
  164.    {
  165.    lpumb->ofn.lpstrTitle = (LPSTR)"Move Archive To";
  166.    lpumb->ofn.lpstrInitialDir =
  167.    (LPSTR)(!szTargetDirName[0] ? NULL : szTargetDirName);
  168.    }
  169. else
  170.    if (rename_flag)
  171.       {
  172.       lpumb->ofn.lpstrTitle = (LPSTR)"Rename Archive To";
  173.       lpumb->ofn.lpstrInitialDir = (LPSTR)((uf.fUnzipToZipDir ||
  174.          !lpumb->szDirName[0]) ? NULL : lpumb->szDirName);
  175.       }
  176.    else
  177.       {
  178.       lpumb->ofn.lpstrTitle = (LPSTR)"Copy Archive To";
  179.       lpumb->ofn.lpstrInitialDir =
  180.          (LPSTR)(!szTargetDirName[0] ? NULL : szTargetDirName);
  181.       }
  182. lpumb->ofn.Flags = OFN_SHOWHELP | OFN_ENABLEHOOK | OFN_CREATEPROMPT |
  183.    OFN_HIDEREADONLY|OFN_ENABLETEMPLATE|OFN_NOCHANGEDIR;
  184. #ifndef WIN32
  185.                 /* Just using an available pointer */
  186. lpfnSelectDir = MakeProcInstance((FARPROC)CopyFileProc, hInst);
  187. #   ifndef MSC
  188. (UINT CALLBACK *)lpumb->ofn.lpfnHook = (UINT CALLBACK *)lpfnSelectDir;
  189. #   else
  190. lpumb->ofn.lpfnHook = lpfnSelectDir;
  191. #   endif
  192.  
  193. #else
  194. lpumb->ofn.lpfnHook = (LPOFNHOOKPROC)CopyFileProc;
  195. #endif
  196. lpumb->ofn.lpTemplateName = "COPYFILE";   /* see copyfile.dlg   */
  197. uf.fTrailingSlash = FALSE; /* set trailing slash to FALSE */
  198. if (GetSaveFileName(&lpumb->ofn)) /* successfully got dir name ? */
  199.    {
  200.    if (lstrcmpi(lpumb->szUnzipToDirNameTmp, lpumb->szFileName)!=0)
  201.       {
  202.       lstrcpy(tempPath, lpumb->szUnzipToDirNameTmp);
  203.       if (!uf.fTrailingSlash) /* Do we have a trailing slash? */
  204.          { /* No - strip file name off and save it */
  205.          StripDirectory(tempPath);
  206.          lstrcpy(tempFileName, tempPath);
  207.          /* Strip file name off, we've just saved it */
  208.          GetDirectory(lpumb->szUnzipToDirNameTmp);
  209.          }
  210.       uf.fTrailingSlash = FALSE;
  211.       /* Okay, make the directory, if necessary */
  212.       if (MakeDirectory(lpumb->szUnzipToDirNameTmp, FALSE))
  213.          {
  214.          OFSTRUCT ofStrSrc;
  215.          OFSTRUCT ofStrDest;
  216.          HFILE hfSrcFile, hfDstFile;
  217.  
  218.          if (lpumb->szUnzipToDirNameTmp[0] != '\0')
  219.             lstrcat(lpumb->szUnzipToDirNameTmp, "\\");
  220.          lstrcat(lpumb->szUnzipToDirNameTmp, tempFileName);
  221.  
  222.          if (move_flag == TRUE)
  223.             fprintf(stdout, "Moving: \n%s\nto\n%s",
  224.                lpumb->szFileName, lpumb->ofn.lpstrFile);
  225.          else
  226.             if (rename_flag == FALSE)
  227.                fprintf(stdout, "Copying: \n%s\nto\n%s",
  228.                lpumb->szFileName, lpumb->ofn.lpstrFile);
  229.  
  230.          if ((rename_flag == TRUE) &&
  231.             ((lpumb->szFileName[0] == lpumb->ofn.lpstrFile[0]) ||
  232.             (lpumb->ofn.lpstrFile[1] != ':')))
  233.             {
  234.             fprintf(stdout, "Renaming: \n%s\nto\n%s\n",
  235.                lpumb->szFileName, lpumb->ofn.lpstrFile);
  236.             rename(lpumb->szFileName, lpumb->ofn.lpstrFile);
  237.             lstrcpy(lpumb->szFileName, lpumb->ofn.lpstrFile);
  238.             UpdateButtons(hWnd); /* update state of buttons */
  239.             SetCaption(hWnd);
  240.             }
  241.          else if (rename_flag == FALSE)
  242.             {
  243.             hfSrcFile = LZOpenFile(lpumb->szFileName,
  244.                &ofStrSrc, OF_READ);
  245.             hfDstFile = LZOpenFile(lpumb->ofn.lpstrFile,
  246.                &ofStrDest, OF_CREATE);
  247.  
  248.             LZCopy(hfSrcFile, hfDstFile);
  249.  
  250.             LZClose(hfSrcFile);
  251.             LZClose(hfDstFile);
  252.             fprintf(stdout, " ...%s\n", "Done");
  253.             if ((toupper(lpumb->ofn.lpstrFile[0]) != 'A') &&
  254.                (toupper(lpumb->ofn.lpstrFile[0]) != 'B'))
  255.                {
  256.                lstrcpy(szTargetDirName, lpumb->ofn.lpstrFile);
  257.                if ((lpchLast = lstrrchr(szTargetDirName, '\\'))!=0)
  258.                   {
  259.                   *lpchLast = '\0';
  260.                   }
  261.                else if ((lpchLast = lstrrchr(szTargetDirName, ':'))!=0)
  262.                   {
  263.                   *(lpchLast+1) = '\0';
  264.                   lstrcat(szTargetDirName, "\\");
  265.                   }
  266.                }
  267.             }
  268.          if (move_flag)
  269.             {
  270.             /*
  271.             * It's not clear what should be done here, just
  272.             * clear the list box and be done with it, or
  273.             * actually go to the new location of the zipfile.
  274.             * The election at this point is to just be done
  275.             * with it.
  276.             */
  277.             SendMessage(hWndList, LB_RESETCONTENT, 0, 0);
  278.             remove(lpumb->szFileName);
  279.             lpumb->szFileName[0] = '\0';
  280.             UpdateButtons(hWnd); /* update state of buttons */
  281.             }
  282.          }
  283.       }
  284.    }
  285. else /* either real error or canceled */
  286.    {
  287.    DWORD dwExtdError = CommDlgExtendedError(); /* debugging */
  288.  
  289.    if (dwExtdError != 0L) /* if not canceled then real error */
  290.       {
  291.       wsprintf (lpumb->szBuffer, szCantCopyFile, dwExtdError);
  292.       MessageBox (hWnd, lpumb->szBuffer, szAppName, MB_ICONINFORMATION | MB_OK);
  293.       }
  294.    else
  295.       {
  296.       RECT mRect;
  297.  
  298.       WinAssert(hWndMain);
  299.       GetWindowRect(hWndMain, &mRect);
  300.       InvalidateRect(hWndMain, &mRect, TRUE);
  301.       SendMessage(hWndMain, WM_SIZE, SIZE_RESTORED,
  302.          MAKELONG(mRect.right-mRect.left, mRect.top-mRect.bottom));
  303.       UpdateWindow(hWndMain);
  304.       }
  305.    }
  306. #ifndef WIN32
  307.                 /* Just using an available pointer */
  308.    FreeProcInstance(lpfnSelectDir);
  309. #endif
  310. }
  311.